//
//  QinzTimeLine.m
//  QinzTimeLine
//
//  Created by Qinz on 2017/5/23.
//  Copyright © 2017年 Qinz. All rights reserved.

#import "QinzTimeLine.h"
#import "SDAutoLayout.h"
#import "TimeLineCell.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface QinzTimeLine ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation QinzTimeLine

-(void)setSuperView:(UIView *)superView DataArr:(NSMutableArray *)dataArr{
    
    self.frame = superView.bounds;
    [superView addSubview:self];
    
    [self setUp];
    self.dataArr = dataArr;
}
-(void)setUp{
    
    self.tableView = [[UITableView alloc]init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self addSubview:self.tableView];
    
    self.tableView.sd_layout.topEqualToView(self).leftEqualToView(self).bottomEqualToView(self).rightEqualToView(self);
}

#pragma mark -- tableView的代理方法
#pragma mark -- 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
#pragma mark -- 每组返回多少个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
#pragma mark -- 每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TimeLineModel *model = self.dataArr[indexPath.row];
    
    return 60;
    //return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[TimeLineCell class] contentViewWidth:self.frame.size.width];
}
#pragma mark -- 每个cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *nowdate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit;
    comps = [calendar components:unitFlags fromDate:nowdate];
    
    
    
    /*NSLog(@"%ld年",(long)[comps year]);
    NSLog(@"%ld月",(long)[comps month]);
    NSLog(@"%ld日",(long)[comps day]);
    NSLog(@"%ld时",(long)[comps hour]);
    NSLog(@"%ld分",(long)[comps minute]);
    NSLog(@"%ld秒",(long)[comps second]);
    NSLog(@"%ld zhouji", (long)[comps weekday]);*/
    
    NSString *now_hour;
    if (((long)[comps hour]  + 24) % 24 < 10) {
        now_hour = [NSString stringWithFormat:@"0%ld", ((long)[comps hour]  + 24) % 24];
    }
    else {
        now_hour = [NSString stringWithFormat:@"%ld", ((long)[comps hour]  + 24) % 24];
    }
    
    NSString *now_minute;
    if ((long)[comps minute] < 10) {
        now_minute = [NSString stringWithFormat:@"0%ld", (long)[comps minute]];
    }
    else {
        now_minute = [NSString stringWithFormat:@"%ld", (long)[comps minute]];
    }
    
    NSString* nowtime = [NSString stringWithFormat:@"%@:%@", now_hour, now_minute];
    TimeLineCell*cell = [TimeLineCell timeLineCell:tableView];
    // NSString* tar_time = self.dataArr[indexPath.row].
    cell.model = self.dataArr[indexPath.row];
    
    NSString* tartime = cell.model.time;
    
    if ([tartime compare:nowtime options:NSNumericSearch] == NSOrderedDescending)
    {
        cell.pointView.backgroundColor = [UIColor whiteColor];
        cell.pointView.layer.masksToBounds = YES;
        cell.pointView.layer.borderWidth = 1.5;
        cell.pointView.layer.borderColor = [UIColor colorWithRed:116.0/255 green:104.0/255 blue:190.0/255 alpha:1].CGColor;
    }else
    {
        cell.pointView.backgroundColor = [UIColor colorWithRed:116.0/255 green:104.0/255 blue:190.0/255 alpha:1];
    }
    
    if (indexPath.row != self.dataArr.count - 1) {
        cell.lineView.sd_layout.topSpaceToView(cell.pointView, 0);
        cell.lineView.backgroundColor =  [UIColor colorWithRed:116.0/255 green:104.0/255 blue:190.0/255 alpha:1];
        cell.lineView.hidden = NO;

    }
    else {
        cell.lineView.hidden = YES;
    }
    
    
    /*if (indexPath.row == 0) {
     cell.lineView.sd_layout.topSpaceToView(cell.pointView, 0);
     cell.lineView.backgroundColor =  [UIColor colorWithRed:116.0/255 green:104.0/255 blue:190.0/255 alpha:1];
     cell.pointView.backgroundColor = [UIColor colorWithRed:116.0/255 green:104.0/255 blue:190.0/255 alpha:1];
     }else{
     cell.lineView.sd_layout.topSpaceToView(cell.pointView, 0);
     cell.pointView.backgroundColor = [UIColor whiteColor];
     cell.pointView.layer.masksToBounds = YES;
     cell.pointView.layer.borderWidth = 1.5;
     cell.pointView.layer.borderColor = [UIColor colorWithRed:116.0/255 green:104.0/255 blue:190.0/255 alpha:1].CGColor;
     cell.lineView.backgroundColor =  [UIColor colorWithRed:116.0/255 green:104.0/255 blue:190.0/255 alpha:1];
     }*/
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -- 选择每个cell执行的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]init];
    
    headerView.backgroundColor = [UIColor whiteColor];
    //headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *label = [[UILabel alloc]init];
    label.textColor = [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:1];
    label.font = [UIFont systemFontOfSize:15];
    label.frame = CGRectMake(1.35 * SCREEN_WIDTH / 4, 10, SCREEN_WIDTH / 2, 20);
    [headerView addSubview:label];
    
    if (section == 0) {
        label.text = @"日程安排";
        
    }
    
    return headerView;
    
}


@end
